package com.zxl.always.on.display.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.ui.service.ScreenService;

/**
 * Created by Administrator on 2017/12/27.
 */

public class KeepLiveActivity extends Activity {

    private static KeepLiveActivity sInstance;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = new View(this);
        view.setBackgroundColor(Color.TRANSPARENT);
        setContentView(view);
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.x = 0;
        layoutParams.y = 0;
        layoutParams.width = 10;
        layoutParams.height = 10;
        window.setAttributes(layoutParams);
        sInstance = this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ScreenService.startCoreService(this);
        sInstance = null;
    }

    public static void start() {
        try {
            ApplicationHelper.getContext().startActivity(new Intent(ApplicationHelper.getContext(), KeepLiveActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void stop() {
        try {
            if (null != sInstance) {
                sInstance.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
